package com.example.springboot.controller;

import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.pojo.entity.User;
import com.example.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/selectByPage")
    public Result page(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize,
                       @RequestParam String username,
                       @RequestParam String role){
        return userService.pageUser(pageNum,pageSize,username,role);
    }

    @HoneyLogs(operation = "更新用户信息",type = LogType.UPDATE)
    @PutMapping("/update")
    public Result updateById(@RequestBody User User) {
      return userService.update(User);
    }

    @HoneyLogs(operation = "新增用户",type = LogType.ADD)
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @HoneyLogs(operation = "删除用户",type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        return userService.deleteUser(id);
    }

    @HoneyLogs(operation = "批量删除用户",type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        return userService.deleteUsers(ids);
    }
}
